
package andre.grids.filesystem.server.configuration;

import andre.grids.filesystem.common.DirectoryOperations;
import andre.grids.filesystem.common.FileOperations;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.activation.*;
import andre.grids.filesystem.server.configuration.*;
import java.rmi.server.UnicastRemoteObject;
import andre.grids.util.log.*;

/**
 *
 * @author andre
 */
public class RmiRegister {
    
    private Registry rmiReg;    
    
    private RmiRegister()
    {
        try
        {
            int port = Integer.parseInt(ConfigurationManager.getBaseConfigInstance().getValue(ServerConfigurationProps.SERVER_PORT_NUMBER));
            rmiReg = LocateRegistry.createRegistry(port);
        }
        catch(Exception e)
        {
            andre.grids.util.log.Log.logException(e, this,true);
        }
    }
    
    private Registry getRmiRegistry()
    {
        return rmiReg;
    }
    
    public static void registerDirectoryObject(DirectoryOperations remoteObj, String regName) {
        RmiRegister register = getInstance();
        try
        {
            Registry reg = register.getRmiRegistry();
            Log.logNormalOperation(Log.OP_TYPE_RMI_REGISTER, "Obtendo registro:", reg);            
            DirectoryOperations stub = (DirectoryOperations)UnicastRemoteObject.exportObject(remoteObj, 0);
            reg.rebind(regName, stub);
            Log.logNormalOperation(Log.OP_TYPE_RMI_REGISTER, "Objeto: " + stub + " com o nome: " + regName, reg);            
        } catch(Exception e) {
            andre.grids.util.log.Log.logException(e, register);
        }
    }
    
    public static void registerFileOperations(FileOperations fileOps, String regName) {
        RmiRegister register = getInstance();
        try
        {            
            Registry reg = register.getRmiRegistry();            
            Log.logNormalOperation(Log.OP_TYPE_RMI_REGISTER, "Obtendo registro:", reg);
            FileOperations stub = (FileOperations)UnicastRemoteObject.exportObject(fileOps, 0);
            reg.rebind(regName, stub);
            Log.logNormalOperation(Log.OP_TYPE_RMI_REGISTER, "Objeto: " + stub + " com o nome: " + regName, reg);
        } catch(Exception e) {
            andre.grids.util.log.Log.logException(e, register);
        }        
    }
    
    private static void uboundObject(String regName)
    {
        try
        {
        }
        catch(Exception e)
        {
            andre.grids.util.log.Log.logException(e, RmiRegister.getInstance(),true);
        }
    }
    
    private static RmiRegister instance;
    private static DirectoryOperations localDirOp;
    private static FileOperations localFileOp;
    
    private static RmiRegister getInstance()
    {
        if (RmiRegister.instance == null)
            RmiRegister.instance = new RmiRegister();
        return instance;
    }
    
    public static void setLocalObjects(DirectoryOperations dirOps, FileOperations fileOps)
    {
        if (dirOps != null)
            localDirOp = dirOps;
        if (fileOps != null)
            localFileOp = fileOps;
    }
    
    public static DirectoryOperations getLocalDirectoryOperation()
    {
        return localDirOp;
    }
    
    public static FileOperations getLocalFileOperations()
    {
        return localFileOp;
    }
}
